package com.ctshk.rpc.system.mapper.provider;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.rpc.system.dto.SysSupplierOrderMsgPageDTO;
import com.ctshk.rpc.system.req.SysSupplierOrderMsgPageReq;

/**
 * 分配组管理查询列表
 *
 * @author 姜斌文
 * @since 2021-03-31
 */
public class SysSupplierOrderMsgProvider {

    public String assignmentMgtList(IPage<SysSupplierOrderMsgPageDTO> page,SysSupplierOrderMsgPageReq sysSupplierOrderMsgPageReq) {
    	String sql = new SQL() {{
                SELECT("t.id,t.rule_name,t.type,t.charge_person_id,t.charge_person_name,t.rule_type,t.message_type,t.system_code,t.status,"
                		+ "(SELECT group_concat(u.user_id) FROM t_supplier_order_message_rule_user u WHERE u.rule_id = t.id )AS \"groupPersonIds\","
                		+ "(SELECT group_concat(u.user_name) FROM t_supplier_order_message_rule_user u WHERE u.rule_id = t.id )AS \"groupPersonNames\"");
                FROM("t_supplier_order_message_rule t");
                if (StringUtils.isNotBlank(sysSupplierOrderMsgPageReq.getRuleName())) {
                    WHERE("t.rule_name like concat('%',#{sysSupplierOrderMsgPageReq.ruleName},'%')");
                }
                if (StringUtils.isNotBlank(sysSupplierOrderMsgPageReq.getSystemCode())) {
                    WHERE("t.system_code like concat('%',#{sysSupplierOrderMsgPageReq.systemCode},'%')");
                }
                if (sysSupplierOrderMsgPageReq.getType() != 0) {
                    WHERE("t.type = #{sysSupplierOrderMsgPageReq.type}");
                }
                if (sysSupplierOrderMsgPageReq.getRuleType() != 0) {
                    WHERE("t.rule_type = #{sysSupplierOrderMsgPageReq.ruleType}");
                }
                if (sysSupplierOrderMsgPageReq.getMessageType() != 0) {
                    WHERE("t.message_type = #{sysSupplierOrderMsgPageReq.messageType}");
                }
                if (null != sysSupplierOrderMsgPageReq.getStatus()) {
                    WHERE("t.status = #{sysSupplierOrderMsgPageReq.status}");
                }
                ORDER_BY("t.gmt_create DESC");
            }}.toString();
            return sql;
    }
}
